<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${privilege == null ? '新增' : '编辑'}产品权限</title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
</head><body>
<!--头部-->
<%@include file="../../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/help/help/save.do">
                    <input type="hidden" name="id" value="${privilege.id}"/>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">上级权限<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input id="parentId" name="parentId" value="${parentPrivilege.id}" hidden>
                                <c:if test="${empty privilege.id}">
                                    <input type="text" class="layui-input" lay-verify="parentId" value="${parentPrivilege.name}" id="selectParent" readonly="readonly" onclick="clickParent()" />
                                </c:if>
                                <c:if test="${not empty privilege.id}">
                                    <input type="text" class="layui-input" lay-verify="parentId" value="${parentPrivilege.name}" id="selectParent" readonly="readonly" />
                                </c:if>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">权限名称<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" lay-verify="name" autocomplete="off" class="layui-input" value="${privilege.name}">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">权限代码<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" lay-verify="code" autocomplete="off" class="layui-input" value="${privilege.code}">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">排序<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="sort" lay-verify="sort" autocomplete="off" class="layui-input" value="${privilege.sort}">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">立即提交</a>
                            <a href="javascript:history.back();" class="layui-btn layui-btn-primary">取消</a>
                        </div>
                    </div>
                </form>
            </div>
        </div><!--底部-->
        <%@include file="../../include/footer.jsp" %>
    </section>
</div>

<script type="text/javascript">
    $(function() {
        layui.use(['form'], function () {
            var form = layui.form;

            //自定义验证规则
            form.verify({
            	parentId: function (value) {
                    if (value == "") {
                        return '请选择父级权限';
                    }
                },
                name: function (value) {
                    if (value == "") {
                        return '请输入权限名称';
                    }
                },
                code: function (value) {
                    if (value == "") {
                        return '请输入权限代码';
                    } else {
                        var isRepeat = ajaxSubmit(value, '${privilege.id}');
                        if (isRepeat) {
                            return "权限代码已存在";
                        }
                    }
                }
            });

            //监听提交
            form.on('submit', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    $.post('${ctx}/charge/privilege/save.do', $('#myForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                location.href = "${ctx}/charge/privilege/list.do";
                            });
                        } else {
                            util.layerMsgError(res.description)
                        }
                    });
                });
                return false;
            });
        });
    });

    function ajaxSubmit(code, id) {
        var isRepeat = false;
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: {
                code: code,
                id: id
            },
            dataType: 'json',
            success: function (result) {
                isRepeat = !result;
            },
            url: '${ctx}/charge/privilege/checkCode.do'
        });
        return isRepeat;
    }

    function clickParent() {
        var id = $("#parentId").val() || '0';
        layer.open({
            type: 2,
            area: ["400px", "300px"],
            title: "选择父级",
            maxmin: true,
            content: "${ctx}/charge/privilege/" + id + "/selectParent.do",
            btn: ['确定', '关闭'],
            yes: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var inputForm = body.find('#inputForm');
                var top_iframe;
                top_iframe = '_parent';
                inputForm.attr("target", top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示

                var parent = iframeWin.contentWindow.doSubmit();
                if (parent != false){
                    $('#parentId').val(parent.id);
                    $('#selectParent').val(parent.name);
                    setTimeout(function () {
                        top.layer.close(index);
                    }, 100);

                }
            },
            cancel: function (index) {
            }
        });
    }
</script>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '产品权限管理',
            href: 'javascript:history.back();'
        },{
            name: '${privilege == null ? '新增' : '编辑'}产品权限',
            curr: true
        }],
        btns:[{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
</script>
</body>
</html>